<?php
/**
 * Draw vote zheng graph
 */

if(!$this->data){return false;}

# draw zheng function
function zheng($chart, $posx, $posy, $width, $height, $color, $step = 5)
{
        $thick = $height / 20;
        if($step > 0)
        {
                imagefilledrectangle($chart, $posx + $width * 0.1, $posy + $height * 0.1, $posx + $width * 0.9, $posy + $height * 0.1 + $thick, $color);
        }
        if($step > 1)
        {
                imagefilledrectangle($chart, $posx + $width * 0.5, $posy + $height * 0.1, $posx + $width * 0.5 + $thick, $posy + $height * 0.9, $color);
        }
        if($step > 2)
        {
                imagefilledrectangle($chart, $posx + $width * 0.5, $posy + $height * 0.4, $posx + $width * 0.8, $posy + $height * 0.4 + $thick, $color);
        }
        if($step > 3)
        {
                imagefilledrectangle($chart, $posx + $width * 0.2, $posy + $height * 0.5, $posx + $width * 0.2 + $thick, $posy + $height * 0.9, $color);
        }
        if($step > 4)
        {
                imagefilledrectangle($chart, $posx + $width * 0.1, $posy + $height * 0.9 - $thick, $posx + $width * 0.9, $posy + $height * 0.9, $color);
        }
}

# graph settings
$gap = 0.1;
$startx = $this->graph['posx'];
$starty = $this->graph['posy'];
$w = $this->graph['width'];
$h = $this->graph['height'];

$font_color = $this->colors['list'][1];

$zheng_number = ceil(max($this->data) / 5);

$keys_number = count($this->scale['keys']);
$unitx = $w / $keys_number;
$zheng_height = ($h - $this->desc['size'] * 4) / $zheng_number;
$zheng_width = $unitx * (1 - 2 * $gap);
$zheng_gap = $unitx * $gap;

# write keys and values 
for($i = 0; $i < $keys_number; $i++)
{
	# key
	imagettftext($this->chart, $this->desc['size'], $this->desc['angle'], $startx + $unitx * $i + $column_gap, $starty + $this->desc['size'] * 1.5, $font_color, $this->desc['font'], $this->scale['keys'][$i]);
        #value
	imagettftext($this->chart, $this->desc['size'], $this->desc['angle'], $startx + $unitx * $i + $column_gap, $starty + $h - $this->desc['size'] / 2, $font_color, $this->desc['font'], $this->data[$i]);
}

# draw zheng
for($i = 0; $i < $keys_number; $i++)
{
        $zheng_x = $startx + $unitx * $i + $column_gap;
        $zheng_y = $starty + $this->desc['size'] * 2; 
        for($j = $this->data[$i]; $j > 0; $j = $j - 5)
        {
                if($j <= 5)
                {
                       zheng($this->chart, $zheng_x, $zheng_y, $zheng_width, $zheng_height, $font_color, $j); 
                }
                else
                {
                        zheng($this->chart, $zheng_x, $zheng_y, $zheng_width, $zheng_height, $font_color, 5);
                }
                $zheng_y += $zheng_height;
        }        
}	
?>
